﻿using BCGM.MedicalWisdom.IServices.Hospitals;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Model.Models.Hospitals;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace BCGM.MedicalWisdom.Api.Controllers.Hospitals
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class HospitalDepartmentController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IHospitalDepartmentServices _hospitalDepartmentServices;

        public HospitalDepartmentController(IHospitalDepartmentServices HospitalDepartmentServices)
        {
            _hospitalDepartmentServices = HospitalDepartmentServices;
        }

        [HttpGet]
        public async Task<MessageModel<PageModel<HospitalDepartment>>> Get(int page = 1, string key = "", int intPageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            Expression<Func<HospitalDepartment, bool>> whereExpression = a => true;

            return new MessageModel<PageModel<HospitalDepartment>>()
            {
                msg = "获取成功",
                success = true,
                response = await _hospitalDepartmentServices.QueryPage(whereExpression, page, intPageSize)
            };

        }

        [HttpGet("{id}")]
        public async Task<MessageModel<HospitalDepartment>> Get(string id)
        {
            return new MessageModel<HospitalDepartment>()
            {
                msg = "获取成功",
                success = true,
                response = await _hospitalDepartmentServices.QueryById(id)
            };
        }

        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] HospitalDepartment request)
        {
            var data = new MessageModel<string>();

            var id = await _hospitalDepartmentServices.Add(request);
            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }

        //[HttpPut]
        //public async Task<MessageModel<string>> Put([FromBody] HospitalDepartment request)
        //{
        //    var data = new MessageModel<string>();
        //    data.success = await _hospitalDepartmentServices.Update(request);
        //    if (data.success)
        //    {
        //        data.msg = "更新成功";
        //        data.response = request?.id.ObjToString();
        //    }

        //    return data;
        //}

        //[HttpDelete]
        //public async Task<MessageModel<string>> Delete(int id)
        //{
        //    var data = new MessageModel<string>();
        //    var model = await _hospitalDepartmentServices.QueryById(id);
        //    model.IsDeleted = true;
        //    data.success = await _departmentServices.Update(model);
        //    if (data.success)
        //    {
        //        data.msg = "删除成功";
        //        data.response = model?.Id.ObjToString();
        //    }

        //    return data;
        //}
    }
}